
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Forecast.Business;
using Forecast.Database;

public partial class Traders : System.Web.UI.Page
{
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataView view = GridView1.DataSource as DataView;
        
        if (ViewState["sortColumn"].ToString() == e.SortExpression.ToString())
        {
            if ("DESC" == ViewState["sortDirection"].ToString())
            {
                ViewState["sortDirection"] = "ASC";
            }
            else
            {
                ViewState["sortDirection"] = "DESC";
            }
        }
        else
        {
            ViewState["sortColumn"] = e.SortExpression.ToString();
            ViewState["sortDirection"] = "DESC";
        }

        view.Sort = ViewState["sortColumn"].ToString() + " " + ViewState["sortDirection"].ToString();

        GridView1.DataSource = view;
        GridView1.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState["sortColumn"] = " ";
            ViewState["sortDirection"] = " ";
        }

        Activity activity = new Activity(ConfigurationManager.ConnectionStrings["ForecastDB"].ConnectionString);
        DataTable table = activity.GetTradersOverview();
        DataView view = new DataView(table);
        view.Sort = "[" + TradersOverview.Columns.TotalValue.ToString() + "] DESC";

        GridView1.DataSource = view;
        GridView1.DataBind();
    }

    protected string GetLastActivityTime(DateTime activityDate)
    {
        if (activityDate == DateTime.MinValue)
            return "Never";
        return TimespanExtensions.HowLongAgo(activityDate);
    }   

    protected string GetMedals(int playerId)
    {
        DataView view = GridView1.DataSource as DataView;
        DataTable dataTable = view.Table;

        int[] medals = new int[0];
        string[] medalsInPredictions = new string[0];
        foreach (DataRow row in dataTable.Rows)
        {
            int rowPlayerId = (int)row[TradersOverview.Columns.PlayerId.ToString()];
            if (rowPlayerId != playerId)
                continue;
            medals = (int[])row[TradersOverview.Columns.Medals.ToString()];
            medalsInPredictions = (string[])row[TradersOverview.Columns.MedalsInPredictions.ToString()];
        }

        if (medals.Length == 0)
            return "";
        return GetIcons(medals, medalsInPredictions);
    }

    private string GetIcons(int[] medalsForPlayer, string[] medalsInPredictions)
    {
        string medalIcons = "";
        for (int i = 0, j = medalsForPlayer.Length; i < j; i++)
        {
            medalIcons += GetMedalIcon(medalsForPlayer[i], medalsInPredictions[i]);
        }
        return medalIcons;
    }

    protected string GetMedalIcon(int medalId, string ticker)
    {
        if (medalId == 2) // Predictum
            return "<span title='The most cash earned trading when " + ticker + " succeeded'><img src=\"images/silk/award_star_gold_2.png\"></span>";
        else if (medalId == 3) // Fatalum
            return "<span title='The most cash earned trading when " + ticker + " failed'><img src=\"images/silk/award_star_gold_1.png\"></span>";
        return "";
    }
}
